package sf.r2dbc.rowmapper;

import io.r2dbc.spi.ColumnMetadata;
import io.r2dbc.spi.Row;
import io.r2dbc.spi.RowMetadata;
import sf.spring.util.LinkedCaseInsensitiveMap;

import java.util.Map;

/**
 * 结果集转为Map
 */
public class R2dbcMapRowMapper implements R2dbcRowMapper<Map<String, Object>> {
    public final static R2dbcMapRowMapper INSTANCE = new R2dbcMapRowMapper();

    @Override
    public Map<String, Object> apply(Row rs, RowMetadata metadata) {
        Map<String, Object> result = new LinkedCaseInsensitiveMap<>();
        for (ColumnMetadata cmd : metadata.getColumnMetadatas()) {
            String columnName = cmd.getName();
            result.put(columnName, rs.get(columnName));
        }
        return result;
    }

}
